#!/bin/bash

PATCH_PATH=$1
: ${PATCH_PATH:=.}

SYSTEM_BIT=$($(which getconf) LONG_BIT)

if [ $SYSTEM_BIT -gt 32 ]
then
	POSTFIX=_64
else
	POSTFIX=
fi

if [ -f /etc/issue ]
then
	if [ "$(cat /etc/issue |grep Ubuntu)" != "" -a "$(cat /etc/issue |grep 9.04)" != "" ]
	then
		echo "The current system is Ubuntu 9.04"
		TARGET_PATH="$PATCH_PATH"/Ubuntu904$POSTFIX/
	elif [ "$(cat /etc/issue |grep Fedora)" != "" -a "$(cat /etc/issue |grep 11)" != "" ]
	then
		echo "The current system is Fedora11"
		TARGET_PATH="$PATCH_PATH"/Fedora11$POSTFIX/
	fi
fi

#######Begin: copy the patch module usbserial.ko to replace the orginal one for Fedora 11 crash bug################
if [ "$(cat /etc/issue |grep Fedora)" != "" -a "$(cat /etc/issue |grep 11)" != "" ]
then
		/sbin/modprobe -r option
		/sbin/modprobe -r usbserial
		if [ -f /lib/modules/$(uname -r)/kernel/drivers/usb/usbserial_orig.ko ]
		then
			rm -f /lib/modules/$(uname -r)/kernel/drivers/usb/serial/usbserial.ko
			/sbin/depmod -a
		else
			/bin/mkdir /opt/bakup
			mv 	/lib/modules/$(uname -r)/kernel/drivers/usb/serial/usbserial.ko /opt/bakup/usbserial_orig.ko
			/sbin/depmod -a
		fi
		cp $TARGET_PATH/usbserial.ko /lib/modules/$(uname -r)/kernel/drivers/usb/serial/
		chmod 777 /lib/modules/$(uname -r)/kernel/drivers/usb/serial/usbserial.ko
		/sbin/depmod -a
		/sbin/modprobe option
fi
#######End: copy the patch module usbserial.ko to replace the orginal one for Fedora 11 crash bug################


#######Begin: install the patch kernel module hw_usb-storage for the SD no driver attached bug.################
if [ -f /lib/modules/$(uname -r)/kernel/drivers/usb/storage/hw_usb-storage.ko ]
then
	/sbin/rmmod -f hw_usb-storage
	rm -f /lib/modules/$(uname -r)/kernel/drivers/usb/storage/hw_usb-storage.ko
	/sbin/depmod -a
fi
if [ -f $TARGET_PATH/hw_usb-storage.ko ]
then
cp $TARGET_PATH/hw_usb-storage.ko  /lib/modules/$(uname -r)/kernel/drivers/usb/storage/
chmod a+x /lib/modules/$(uname -r)/kernel/drivers/usb/storage/hw_usb-storage.ko
/sbin/depmod -a
/sbin/modprobe hw_usb-storage
fi
echo "ACTION==\"add\", SUBSYSTEMS==\"usb\", ATTRS{modalias}==\"usb:v12D1*ip50\", RUN+=\"/sbin/modprobe hw_usb-storage\"">> /etc/udev/rules.d/10-Huawei-Datacard.rules
#######End: install the patch kernel module hw_usb-storage for the SD no driver attached bug.################
